package br.great.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import br.great.beans.Person;


/**
 * PhotoMap
 * Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
 */
/**
 * @author Developpement of PhotoMap
 */
public class DAO_Friend {

    /** method description
     * @param var signification
     * @exception exception class name
     * @return return_value return signification
     * @see package.class#member label
     */
    private static volatile DAO_Friend instaceDAO_Friends = new DAO_Friend();

    /** The private constructor of DAO_Friends for singleton
     */
    private DAO_Friend() {
    }

    /** The method for get the instance of the class DAO_Friends
     * @return DAO_Friends instaceDAO_Friends
     */
    public static DAO_Friend getInstance() {
        //if(instaceDAO_Friends==null)
        synchronized (DAO_Friend.class) {
            if (instaceDAO_Friends == null) {
                instaceDAO_Friends = new DAO_Friend();
            }
        }
        return instaceDAO_Friends;
    }

    public Vector<String> getNearByFriends(int id_multimedia, String foaf_profil) {
        Vector<String> vs = new Vector<String>();
        PgConnect conn = PgConnect.getInstance();
        String getNearByPersons = "SELECT u.login" +
            " FROM NEAR_BY_BLUETOOTH nbb, users u, foaf_knows fw" +
            " WHERE nbb.ADDRESS_BLUETOOTH = u.MAC_ADD_BLUETOOTH" +
            " AND fw.login=u.login" +
            " AND fw.foaf_profil='" + foaf_profil + "'" +
            " AND nbb.id_multimedia=" + id_multimedia;
        ResultSet rs = conn.executeQuery(getNearByPersons);
        try {
            if (rs != null) {
                while (rs.next()) {
                    vs.add(rs.getString("login"));
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Friend.class.getName()).log(Level.SEVERE, null, ex);
        }
        return vs;
    }

    public Vector<String> getFriendsOfFriend(int id_multimedia, String login) {
        Vector<String> vs = new Vector<String>();
        PgConnect conn = PgConnect.getInstance();
        String getFOF = "SELECT u.login" +
            " FROM NEAR_BY_BLUETOOTH nbb, users u,foaf_knows fw, foaf_knows fof" +
            " WHERE nbb.ADDRESS_BLUETOOTH = u.MAC_ADD_BLUETOOTH" +
            " AND fw.login=u.login" +
            " AND fof.login='" + login + "'" +
            " AND fw.foaf_profil=fof.foaf_profil " +
            " AND nbb.id_multimedia=" + id_multimedia;
//System.out.println(getFOF);
        ResultSet rs = conn.executeQuery(getFOF);
        try {
            if (rs != null) {
                while (rs.next()) {
                    vs.add(rs.getString("login"));
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Friend.class.getName()).log(Level.SEVERE, null, ex);
        }
        return vs;
    }

    public Vector<Person> getFriendsInCollection(int id_collection, String login) {
        Vector<Person> vp = new Vector<Person>();
        PgConnect conn = PgConnect.getInstance();
        String getNearByPersons = "SELECT distinct u.login, u.photo" +
            " FROM NEAR_BY_BLUETOOTH nbb, users u, foaf_knows fw, collection_has_multimedia cm" +
            " WHERE nbb.ADDRESS_BLUETOOTH = u.MAC_ADD_BLUETOOTH" +
            " AND fw.login='" + login + "'" +
            " AND nbb.id_multimedia=cm.id_multimedia" +
            " AND fw.foaf_profil=u.foaf_profil" +
            " AND cm.id_collection=" + id_collection;
        ResultSet rs = conn.executeQuery(getNearByPersons);
        try {
            if (rs != null) {
                while (rs.next()) {
                    Person p = new Person();
                    p.setHasLogin(rs.getString("login"));
                    p.setURLPhoto(rs.getString("photo"));
                    vp.add(p);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Friend.class.getName()).log(Level.SEVERE, null, ex);
        }
        return vp;
    }

//    public void insertFriend(String FOAF, String login) {
//        String insertFriend = "INSERT INTO FOAF_KNOWS VALUES('"+FOAF+"','"+login+"')";
//        PgConnect conn = PgConnect.getInstatance();
//        conn.executeQuery(insertFriend);
//    }
    public Vector getAllFoafPerson() {
        Vector vPerson = new Vector();
        String getAllPerson = "SELECT * FROM FOAF_PERSON;";
        PgConnect conn = PgConnect.getInstance();
        ResultSet rs = conn.executeQuery(getAllPerson);
        try {
            while (rs.next()) {
                Person p = new Person();
                p.setURLFOAFProfil(rs.getString("FOAF_PROFIL"));
                p.setFirstName(rs.getString("firstname"));
                p.setName(rs.getString("name"));
                vPerson.add(p);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Friend.class.getName()).log(Level.SEVERE, null, ex);
        }
        return vPerson;
    }
//    public static void main(String[] args){
//        Vector v = DAO_Friend.instaceDAO_Friends.getAllFoafPerson();
//        for(int i=0; i<v.size(); i++){
//            Person p = (Person)v.get(i);
//            System.out.println(p.getURL_FOAFProfil());
//        }
//    }
}
